package com.molyfun.parents.activity.classroom;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.fragment.dialog.CommonInputDialogFragment;
import com.molyfun.parents.fragment.dialog.CommonMonthInputDialogFragment;
import com.molyfun.parents.rest.bean.NewTeamEvaluateTrainingListResponse;

import java.util.ArrayList;
import java.util.List;

public class NewTeamEvaluateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;

    private ArrayList<NewTeamEvaluateTrainingListResponse.NewTeamEvaluateBean> mList;

    private FragmentManager mFragmentManager;

    public NewTeamEvaluateAdapter(Context context, FragmentManager fragmentManager) {
        this.mContext = context;
        this.mFragmentManager = fragmentManager;
    }

    public void refreshData(ArrayList<NewTeamEvaluateTrainingListResponse.NewTeamEvaluateBean> records) {
        this.mList = records;
        notifyDataSetChanged();
    }

    public void clearAll() {
        if (mList != null) {
            mList.clear();
            notifyDataSetChanged();
        }
    }

    public ArrayList<NewTeamEvaluateTrainingListResponse.NewTeamEvaluateBean> getList(){
        return mList;
    }

    public void addAll(List<NewTeamEvaluateTrainingListResponse.NewTeamEvaluateBean> list) {
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SignUpEvaluateSheetHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_new_team_evaluate, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final SignUpEvaluateSheetHolder mSignUpEvaluateSheetViewHolder = (SignUpEvaluateSheetHolder) holder;
        mSignUpEvaluateSheetViewHolder.mAnswerContainer.removeAllViews();

        final NewTeamEvaluateTrainingListResponse.NewTeamEvaluateBean newTeamEvaluateBean = mList.get(position);

        mSignUpEvaluateSheetViewHolder.mChoiceQuestion.setText(newTeamEvaluateBean.getName());
        for(NewTeamEvaluateTrainingListResponse.NewTeamEvaluateBean.TraininglistBean traininglistBean : newTeamEvaluateBean.getTraininglist()){
            addNewTeamEvaluateView(mSignUpEvaluateSheetViewHolder.mAnswerContainer, traininglistBean);
        }
        mSignUpEvaluateSheetViewHolder.mEndMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new CommonMonthInputDialogFragment.CommonMonthInputDialogBuilder("")
                        .setOnCommonInputCallback(new CommonInputDialogFragment.OnCommonInputCallback() {
                            @Override
                            public void onInputConfirm(String content) {
                                mSignUpEvaluateSheetViewHolder.mEndMonth.setText(content);
                                newTeamEvaluateBean.setBegintime(content);
                            }
                        })
                        .build(mFragmentManager.beginTransaction());
            }
        });
    }

    private void addNewTeamEvaluateView(ViewGroup viewGroup, final NewTeamEvaluateTrainingListResponse.NewTeamEvaluateBean.TraininglistBean traininglistBean){
        View fillbankView = LayoutInflater.from(mContext).inflate(R.layout.layout_new_team_sheet_fill_blanks_cell, null, false);
        TextView titleTv = (TextView) fillbankView.findViewById(R.id.fill_title);
        titleTv.setText(traininglistBean.getName());
        final TextView beginMonth = (TextView) fillbankView.findViewById(R.id.begin_month);
        final TextView endMonth = (TextView) fillbankView.findViewById(R.id.end_month);
        beginMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new CommonMonthInputDialogFragment.CommonMonthInputDialogBuilder("")
                        .setOnCommonInputCallback(new CommonInputDialogFragment.OnCommonInputCallback() {
                            @Override
                            public void onInputConfirm(String content) {
                                beginMonth.setText(content);
                                traininglistBean.setBegintime(content);
                            }
                        })
                        .build(mFragmentManager.beginTransaction());
            }
        });

        endMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new CommonMonthInputDialogFragment.CommonMonthInputDialogBuilder("")
                        .setOnCommonInputCallback(new CommonInputDialogFragment.OnCommonInputCallback() {
                            @Override
                            public void onInputConfirm(String content) {
                                endMonth.setText(content);
                                traininglistBean.setEndtime(content);
                            }
                        })
                        .build(mFragmentManager.beginTransaction());

            }
        });
        viewGroup.addView(fillbankView);
    }


    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }
        return mList.size();
    }
}
